<?php

namespace App\Notifications\OrderNotifications;

use App\Common\Constant\Params;
use App\Models\Master;
use App\Models\Merchant;
use App\Models\Order;
use App\Models\OrderRefund;
use App\Notifications\Channels\MerchantMessageChannel;
use App\Notifications\Channels\SmsChannel;
use App\Notifications\OrderNotification;
use App\Services\IMasterService;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class OrderRefundNotification extends Notification implements OrderNotification
{
    use Queueable;

    private $refund;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct(OrderRefund $refund)
    {
        $this->refund = $refund;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return [MerchantMessageChannel::class,SmsChannel::class];
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function messageToMerchant(Merchant $merchant)
    {
        if ($this->refund->refund_type == Params::ORDER_REFUND_RESULT_DISAGREE){//拒绝退款
            $message = <<<EOT
亲爱的%s，您好！您于%s提交的(订单：%s)退款申请,已被师傅拒绝退款。(建议您与师傅再次沟通协商处理，协商不成可以申请仲裁)。
师傅拒绝退款说明：%s
EOT;
            $message = sprintf($message,
                $merchant->me_username,
                $this->refund->add_time,
                $this->refund->order->order_number,
                $this->refund->refund_refuse_reason);
            $title = '师傅拒绝退款提醒';
        }else{//同意退款
            $message = <<<EOT
亲爱的%s，您好！您于%s提交的(订单：%s)退款申请,师傅已经同意退款。
退款形式：%s
退款金额：%.2f
EOT;
            $message = sprintf($message,
                $merchant->me_username,
                $this->refund->add_time,
                $this->refund->order->order_number,
                $this->refund->refund_type_text,
                $this->refund->refund_amount);
            $title = '退款申请通过提醒';
        }
        return [$title,nl2br($message)];
    }

    /**
     * 发送短信
     * @param Master $master
     * @return array($phoneNum,$message)
     */
    public function toSms(Merchant $merchant)
    {
        if ($this->refund->refund_result_type == Params::ORDER_REFUND_RESULT_DISAGREE){//拒绝退款
            $message = <<<EOT
【%s】您的家具服务订单%s，师傅已拒绝退款。您可以与师傅重新沟通后再提交申请退款，或直接在原退款详情页申请客服介入处理。相关疑问请联系师傅。
EOT;
            $message = sprintf($message,
                config('app.name'),
                $this->refund->order->order_number);
        }else{//同意退款
            if ($this->refund->refund_method == Params::REFUND_METHOD_ORIGIN){//原路返回
                $message = <<<EOT
【%s】您的家具服务订单%s，师傅已经同意退款，%.2f元将原路返回您的支付账户，请查收。如有疑问请致电0779-3931900
EOT;
            }else{//退回钱包
                $message = <<<EOT
【%s】您的家具服务订单%s，师傅已经同意退款，%.2f元已返还到您的乐帮到家账户，请查收。如有疑问请致电0779-3931900
EOT;
            }
            $message = sprintf($message,
                config('app.name'),
                $this->refund->order->order_number,
                $this->refund->refund_amount);
        }
        return [$merchant->me_phone,$message];
    }
}
